class HttpRequest {
	constructor(config = {}) {
		this.defaultConfig = {
			baseUrl: '',
			timeout: 60000,
			header: {
				'Content-Type': 'application/json'
			},
			...config
		};
	}
	async request(method, url, data = {}, config = {}) {
		const mergedConfig = {
			...this.defaultConfig,
			...config
		};
		const fullUrl = url.startsWith('http') ? url : `${mergedConfig.baseUrl}${url}`;
		let requestConfig = {
			url: fullUrl,
			method: method.toUpperCase(),
			data,
			timeout: mergedConfig.timeout,
			header: {
				...mergedConfig.header,
				...(config.header || {})
			}
		};
		const token = uni.getStorageSync('token');
		if (token) {
			requestConfig.header.Token = `${token}`;
		}
		return new Promise((resolve, reject) => {
			uni.showLoading({
				title: '数据加载中!'
			});
			const requestTask = uni.request({
				...requestConfig,
				success: async (response) => {
					resolve(response.data);
				},
				fail: (error) => {
					reject({
						code: 'NETWORK_ERROR',
						message: '网络请求失败',
						error
					});
				},
				complete() {
					setTimeout(()=>{
						uni.hideLoading()
					},1000)
				}
			});
		});
	}

get(url, params = {}, config = {}) {
	return this.request('GET', url, params, config);
}
post(url, data = {}, config = {}) {
	return this.request('POST', url, data, config);
}
}
export const baseUrl = 'http://172.18.1.175:9093/api/mini';

const http = new HttpRequest({
	baseUrl,
});
export default http